package com.hzit.controller;

import cn.hutool.core.util.StrUtil;
import com.hzit.pojo.Classes;
import com.hzit.pojo.PageResult;
import com.hzit.pojo.Student;
import com.hzit.pojo.StudentVo;
import com.hzit.service.ClassesService;
import com.hzit.service.StudentService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Struct;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	private ClassesService classesService;
	@Override
	public void init() throws ServletException {
		//1. 通过spring-web包下的WebApplicationContextUtils工具类得到spring容器
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		//2. 得到spring容器中的javabean
		studentService = wac.getBean(StudentService.class);
		classesService = wac.getBean(ClassesService.class);

	}
	//2. 根据请求参数调用不同的处理方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//2.1 处理中文乱码
		req.setCharacterEncoding("UTF-8");
		//2.2 得到请求处理参数cmd
		String cmd = req.getParameter("cmd");
		//2.3 根据参数内容，调用不同的方法
		if(StrUtil.isNotBlank(cmd)){
			if ("list".equals(cmd)) {
				list(req,resp);
			}
			if("findByPage".equals(cmd)){
				findByPage(req,resp);
			}
			if("search".equals(cmd)){
				search(req,resp);
			}
		}
	}
	//3. 学生列表
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//3.1 得到学生列表
		List<Student> studentList = studentService.findAll();
		//3.2 将学生集合放到request请求作用域中
		req.setAttribute("students",studentList);
		//3.3 进行页面的转发(跳转)
		req.getRequestDispatcher("/index.jsp").forward(req,resp);
	}
	//4. 分页查询
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//4.1 得到分页参数
		String pageS = req.getParameter("page");
		String pageSizeS = req.getParameter("pageSize");
		int page = 1;
		int pageSize = 3;
		if(StrUtil.isNotBlank(pageS)){
			page = new Integer(pageS);
		}
		if(StrUtil.isNotBlank(pageSizeS)){
			pageSize = new Integer(pageSizeS);
		}
		//4.2 开始分页
		PageResult<Student> pageResult = studentService.findByPage(page,pageSize);
		//4.3 将学生集合放到request请求作用域中
		req.setAttribute("pr",pageResult);
		//4.4 查询所有班级
		List<Classes> classes = classesService.findAll();
		req.setAttribute("classes",classes);
		//4.5 进行页面的转发(跳转)
		req.getRequestDispatcher("/index.jsp").forward(req,resp);
	}
	//5. 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//5.1 得到分页参数
		String pageS = req.getParameter("page");
		String pageSizeS = req.getParameter("pageSize");
		int page = 1;
		int pageSize = 3;
		if(StrUtil.isNotBlank(pageS)){
			page = new Integer(pageS);
		}
		if(StrUtil.isNotBlank(pageSizeS)){
			pageSize = new Integer(pageSizeS);
		}
		//5.2 得到查询参数
		String sname = req.getParameter("sname");
		String addr = req.getParameter("addr");
		String cidS =  req.getParameter("cid");
		Integer cid = 0;
		if(StrUtil.isNotBlank(cidS)){
			cid = new Integer(cidS);
		}
		StudentVo vo = new StudentVo(sname,addr,cid);
		//5.3 调用查询方法
		PageResult<Student> pageResult = studentService.search(page,pageSize,vo);
		//5.4 将上面的对象放到request作用域中
		req.setAttribute("pr",pageResult);
		//5.5 查询所有班级
		List<Classes> classes = classesService.findAll();
		req.setAttribute("classes",classes);
		// req.setAttribute("vo",vo);
		//5.6 进行页面的转发(跳转)
		req.getRequestDispatcher("/index.jsp").forward(req,resp);


	}
}
